﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using StartupSolution.ViewModel;
using Infragistics.Controls.Grids;

namespace StartupSolution.Views.Grids
{
    public partial class Selection : UserControl
    {
        public Selection()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(Selection_Loaded);
        }

        void Selection_Loaded(object sender, RoutedEventArgs e)
        {
            /// <summary> 
            /// This method will load data from a XML document and populate it as XamGrid ItemSource
            /// </summary>
            LoadXMLData();
        }

        private void LoadXMLData()
        {
            // 	Loads the XML file and creates a XDocument object
            XDocument doc = XDocument.Load("Datasource/Products.xml");

            // Run LINQ query to retrieve the necessary data
            var data = (from d in doc.Descendants("Products")
                        select new Product
                        {
                            SKU = d.Element("ProductID").Value,
                            Name = d.Element("ProductName").Value,
                            Category = d.Element("Category").Value,
                            Supplier = d.Element("Supplier").Value,
                            QuantityPerUnit = d.Element("QuantityPerUnit").Value
                        }).ToList<Product>();

            // Set the ItemsSource to generate the columns.
            igGrid.ItemsSource = data;
        }

        private void igGrid_RowSelectorClicked(object sender, RowSelectorClickedEventArgs e)
        {
            // Row selectors are primarily used for selecting or activating rows.
            // However clicking a row selector allows you to perform any custom action on that row by handling the row selector’s event.

            // TODO: Implement your custom logic for handling RowSelectorClicked event in XamGrid control

            // The example will display some extra data using the RowSelectorClicked event.
            // The event fires after the row selector is clicked. 

            Product product = e.Row.Data as Product;
            ListBoxItem item = new ListBoxItem();
            item.Content = "SKU: " + product.SKU + "; QuantityPerUnit: " + product.QuantityPerUnit;

            lBoxDetails.Items.Add(item);
        }

        private void igGrid_SelectedRowsCollectionChanged(object sender, SelectionCollectionChangedEventArgs<SelectedRowsCollection> e)
        {
            // This event is fired when a row has been added to or removed from the SelectedRows collection.

            // TODO: Implement your custom logic for handling SelectedRowsCollectionChanged event in XamGrid control

            txtBlock.Text = "Selected items: " + e.NewSelectedItems.Count.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lBoxDetails.Items.Clear();
        }
    }
}
